#include "QtUI/CloseProjectCommand.qth"
#include "QtUI/CloseProjectCommand.moh"
#include "QtUI/ProjectCommands.qth"
#include "Ctrl/ControlCenter.hpp"
#include "Ctrl/ProjectController.hpp"
#include "Storage/StorageFactory.hpp"
#include <QtGui/QMessageBox>

namespace QtUI
{

CloseProjectCommand::CloseProjectCommand( QAction* pBoundAction)
	: UICommandBase( pBoundAction)
	, Impl::ProjectRelatedCommandEnabler( pBoundAction)
{
}

// ---------------------------------------------------------------------------------------------------------------------

bool CloseProjectCommand::execute( bool /*= false*/)
{
	if( this->canCloseProject())
	{
		Ctrl::ControlCenter::instance().prjController().setControlledProject( NULL, NULL);
	}
	else
	{
		return false;
	}
}

// ---------------------------------------------------------------------------------------------------------------------

bool CloseProjectCommand::canCloseProject() const
{
	if( Ctrl::ControlCenter::instance().prjController().hasControlledProject()
		&& Ctrl::ControlCenter::instance().prjController().storageFactory().hasUnsavedChanges())
	{
		int result = QMessageBox::question( NULL, 
			tr( "Saving Project Confirmation", "Title for saving project confirmation"),
			tr( "There are unsaved changes to the current project. Do you want to save?", "Question message for saving the project"), 
			QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, 
			QMessageBox::Yes);
		if( result == QMessageBox::Yes)
		{
			return ProjectCommands::getSaveProjectCommand().execute();
		}
		else if( result == QMessageBox::No)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	return true;
}

} // namespace QtUI
